import { Get, Controller, Response, HttpStatus, Param, Post, Body, Put, Delete, UsePipes } from '@nestjs/common'
import { UserService } from './cat.service'
import { User } from 'interface/user/user.interface'
import { ValidationPipe } from '../../share/pipe/validate'

@Controller('/user')
export class UserController {
  constructor(private readonly userService: UserService) {}
  @Get('/findAll')
  async findAll(@Response() res) {
    const users = await this.userService.findAll()
    res.status(HttpStatus.OK).json(users)
  }

  @Get('/findOne/:id')
  async findOne(@Response() res, @Param('id') id) {
    const user = await this.userService.findOne(+id)
    res.status(HttpStatus.OK).json(user)
  }

  @Post('/create')
  @UsePipes(ValidationPipe)
  async create(@Body() user: User, @Response() res) {
    console.log(user)
    const newUser = await this.userService.create(user)
    // return newUser
    res.status(HttpStatus.CREATED).json(newUser)
  }

  @Put('/edit')
  async edit(@Body() user, @Response() res) {
    const newUser = await this.userService.edit(user)
    res.status(HttpStatus.OK).json(newUser)
  } 

  @Delete('/remove/:id')
  async remove(@Param('id') id, @Response() res) {
    const bool = await this.userService.remove(+id)
    res.status(HttpStatus.OK).json(bool)
  }
}